package com.eedi.org.healthinfo.service;

import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.org.healthinfo.controller.vo.OrgHealthInfoPageReqVO;
import com.eedi.org.healthinfo.controller.vo.OrgHealthInfoSaveReqVO;
import com.eedi.org.healthinfo.controller.vo.OrgHealthInfoUpdateStatusReqVO;
import com.eedi.org.healthinfo.dal.dataobject.OrgHealthInfoDO;
import com.eedi.org.healthinfo.dal.mapper.OrgHealthInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.org.enums.OrgErrorCodeConstants.HEALTH_INFO_NOT_EXISTS;

/**
 * 健康资讯 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrgHealthInfoServiceImpl implements OrgHealthInfoService {

    @Resource
    private OrgHealthInfoMapper orgHealthInfoMapper;

    @Override
    public String createHealthInfo(OrgHealthInfoSaveReqVO createReqVO) {
        // 插入
        OrgHealthInfoDO healthInfo = BeanUtils.toBean(createReqVO, OrgHealthInfoDO.class);
        orgHealthInfoMapper.insert(healthInfo);
        // 返回
        return healthInfo.getOrgHealthInfoId();
    }

    @Override
    public void updateHealthInfo(OrgHealthInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateHealthInfoExists(updateReqVO.getOrgHealthInfoId());
        // 更新
        OrgHealthInfoDO updateObj = BeanUtils.toBean(updateReqVO, OrgHealthInfoDO.class);
        orgHealthInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteHealthInfo(String orgHealthInfoId) {
        // 校验存在
        validateHealthInfoExists(orgHealthInfoId);
        // 删除
        orgHealthInfoMapper.deleteById(orgHealthInfoId);
    }

    private void validateHealthInfoExists(String orgHealthInfoId) {
        if (orgHealthInfoMapper.selectById(orgHealthInfoId) == null) {
            throw exception(HEALTH_INFO_NOT_EXISTS);
        }
    }

    @Override
    public OrgHealthInfoDO getHealthInfo(String orgHealthInfoId) {
        return orgHealthInfoMapper.selectById(orgHealthInfoId);
    }

    @Override
    public PageResult<OrgHealthInfoDO> getHealthInfoPage(OrgHealthInfoPageReqVO pageReqVO) {
        return orgHealthInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateHealthInfoStatus(OrgHealthInfoUpdateStatusReqVO reqVO) {
        // 校验存在
        validateHealthInfoExists(reqVO.getOrgHealthInfoId());
        // 更新
        OrgHealthInfoDO updateObj = BeanUtils.toBean(reqVO, OrgHealthInfoDO.class);
        orgHealthInfoMapper.updateById(updateObj);
    }

    @Override
    public List<OrgHealthInfoDO> getHealthInfoByGroupId(String orgHealthInfoGroupId) {
        return orgHealthInfoMapper.selectByGroupId(orgHealthInfoGroupId);
    }

}
